package com.xinyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinyu.config.FileStorageProperties;
import com.xinyu.domain.Daily;
import com.xinyu.domain.Task;
import com.xinyu.mapper.DailyMapper;
import com.xinyu.mapper.TaskMapper;
import com.xinyu.mapper.TaskStateHistoryMapper;
import com.xinyu.mapper.UserMapper;
import com.xinyu.service.ITaskService;
import com.xinyu.vo.BaseResult;
import com.xinyu.vo.TaskRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author: XinYu
 * @since 2025-02-17
 */
@Service
@Transactional
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TaskStateHistoryMapper taskStateHistoryMapper;

    @Autowired
    private FileStorageProperties fileStorageProperties; // 注入配置类
    @Override
    public BaseResult queryByCondition(TaskRequest taskRequest) {
        if (taskRequest.getSortColumn() == null || taskRequest.getSortColumn().isEmpty()) {
            taskRequest.setSortColumn("publish_time");
        }
        if (taskRequest.getSortWay() == null || taskRequest.getSortWay().isEmpty()) {
            taskRequest.setSortWay("DESC");
        }
        List<Task> list = taskMapper.queryByCondition(taskRequest);

        for (Task task:list){
            //设置statehistory
            task.setTaskStateHistoryList(taskStateHistoryMapper.selectByTaskIdWithUser(task.getId()));
            //设置图片地址
            if (StringUtils.isNotBlank(task.getPublishUser().getHead()))
                if (!task.getPublishUser().getHead().contains(fileStorageProperties.getBaseUrl()))
                    task.getPublishUser().setHead(fileStorageProperties.getBaseUrl() + task.getPublishUser().getHead());
        }

        Integer countTasks = taskMapper.countTasks(taskRequest);
        return BaseResult.ok(list,countTasks);
    }

    @Override
    public Task findByDailyId(Integer dailyId) {
        Daily daily = dailyMapper.selectById(dailyId);

        Task task = taskMapper.selectById(daily.getTaskId());
        if (task!=null){
            task.setPublishUser(userMapper.selectById(task.getPublishUserId()));
            if (task.getResponsibleUserId()!=null)
                task.setResponsibleUser(userMapper.selectById(task.getResponsibleUserId()));
        }
        //设置图片地址
        if (StringUtils.isNotBlank(task.getPublishUser().getHead()))
            task.getPublishUser().setHead(fileStorageProperties.getBaseUrl() + task.getPublishUser().getHead());
        if (task.getResponsibleUser()!=null)
            if (StringUtils.isNotBlank(task.getPublishUser().getHead()))
                task.getResponsibleUser().setHead(fileStorageProperties.getBaseUrl() + task.getResponsibleUser().getHead());
        //设置statehistory
        task.setTaskStateHistoryList(taskStateHistoryMapper.selectByTaskIdWithUser(task.getId()));

        return task;
    }

    @Override
    public void syncDailyState(Task task) {
        if (task == null || task.getId() == null) {
            return;
        }
        LambdaUpdateWrapper<Daily> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Daily::getTaskId, task.getId())
                .eq(Daily::getIsDelete, 0);

        boolean needUpdate = false;
        if (task.getState() != null) {
            updateWrapper.set(Daily::getState, task.getState());
            needUpdate = true;
        }
        if (StringUtils.isNotBlank(task.getCompletedTime())) {
            updateWrapper.set(Daily::getUpdateTime, task.getCompletedTime());
            needUpdate = true;
        }

        if (needUpdate) {
            dailyMapper.update(null, updateWrapper);
        }
    }
}
